//
//  ConnectModel.m
//  DAY18_综合讲解
//
//  Created by wanli on 14/12/18.
//  Copyright (c) 2014年 wanli. All rights reserved.
//

#import "ConnectModel.h"
#import "Reachability.h"
#import "LocalFileModel.h"
@implementation ConnectModel
- (void)dealloc
{
    [_urlName release];
    [_data release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.data = [NSMutableData data];
    }
    return self;
}

//网络判断
+ (BOOL)judgeConnected{
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
            return NO;
            break;
            
        case ReachableViaWiFi:
            return YES;
            break;
        case ReachableViaWWAN:
            return YES;
            break;
        default:
            break;
    }
    
}

+ (void)connectModelWithUrl:(NSString *)str parmaters:(NSDictionary *)parmaters finished:(ConnectBlock)finishedBlock;
{
    
    BOOL connected = [ConnectModel judgeConnected];
    
    if (connected) {
        ConnectModel *model = [[ConnectModel alloc] init];
//        model.delegate = delegate;
        model.finished = finishedBlock;
        [model startConnectWithUrlStr:str parmaters:parmaters];
        [model release];
    }else{
        //从本地获取数据
        //以网址为文件名,名字中不得包含'/'字符
        NSString *name = [ConnectModel urlWhitStr:str parmaters:parmaters];
        NSData *data = [LocalFileModel readFile:name];
        //将数据回调到主界面
//        [delegate connectFinish:data];
        
        finishedBlock(data);
    }
}

//获取完整的文件名,以网址作为文件名
+ (NSString *)urlWhitStr:(NSString *)urlStr parmaters:(NSDictionary *)parmaters{
    
    NSString *myStr = @"";
    for (NSString *key in [parmaters allKeys]) {
        if ([myStr length] == 0) {
            myStr = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        }else{
            myStr = [NSString stringWithFormat:@"%@&%@=%@", myStr, key, [parmaters objectForKey:key]];
        }
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, myStr];
    //字符串替换, 将'/'替换成空字符
    urlStr = [urlStr stringByReplacingOccurrencesOfString:@"/" withString:@""];
    return urlStr;
}

//www.baidu.com?hello=ii&nihao=oo
//www.baidu.com {"hello":"ii", "nihao":"oo"}

- (void)startConnectWithUrlStr:(NSString *)str parmaters:(NSDictionary *)parmaters{

    
    NSString *myStr = @"";
    for (NSString *key in [parmaters allKeys]) {
        if ([myStr length] == 0) {
            myStr = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        }else{
            myStr = [NSString stringWithFormat:@"%@&%@=%@", myStr, key, [parmaters objectForKey:key]];
        }
    }
    
    str = [NSString stringWithFormat:@"%@%@", str, myStr];
    //urlName用来存储文件的名字
    self.urlName = str;
    
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:request delegate:self
     ];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    [self.data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//    [self.delegate connectFinish:_data];
    self.finished(_data);
    //存储数据
    NSString *name = [ConnectModel urlWhitStr:self.urlName parmaters:nil];
    [LocalFileModel saveFile:_data name:name];

    
}
@end














